﻿<script setup lang="ts">
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const route = useRoute()
const userId = computed(() => route.params.id)

interface UserInfo {
  name: string
  avatar: string
  description: string
  stats: {
    follows: number
    fans: number
    likes: number | string
  }
}

interface Work {
  id: number
  title: string
  image: string
  likes: number
  shares: number
}

const loading = ref(true)
const error = ref(false)
const showOptions = ref(false)
// 新增：举报理由弹框的显示状态
const showReportDialog = ref(false)
// 新增：举报理由选项数组
const reportReasons = ref([
  '色情低俗信息',
  '违禁品信息',
  '政治敏感信息',
  '广告信息',
  '涉嫌辱骂及个人隐私',
  '设未成人不良信息'

])
// 新增：选中的举报理由
const selectedReportReason = ref('')

const userInfo = ref<UserInfo>({
  name: '',
  avatar: '',
  description: '',
  stats: {
    follows: 0,
    fans: 0,
    likes: 0
  }
})

const works = ref<Work[]>([])

const fetchUserContent = async () => {
  loading.value = true
  error.value = false

  try {
    const response = await axios.get(`http://localhost:10086/commity/community-content/getContentByUserId/${userId.value}`)
    const data = response.data
    if (data && data.length > 0) {
      // 设置用户信息
      userInfo.value = {
        name: data[0].nickname,
        avatar: data[0].avatar,
        description: data[0].signature || '爱个性，不爱写...',
        stats: {
          follows: data[0].followCount,
          fans: data[0].followerCount,
          likes: formatNumber(data[0].likeReceivedCount)
        }
      }

      // 设置作品列表
      works.value = data.map(item => ({
        id: item.id,
        title: item.title,
        image: item.imageUrl,
        likes: item.likeCount,
        shares: item.shareCount || 0
      }))
    }
  } catch (error) {
    console.error('获取用户内容失败:', error)
    error.value = true
  } finally {
    loading.value = false
  }
}

const submitReport = () => {
  axios.post('http://localhost:10086/commity/report/submitReport', {
    reason: selectedReportReason.value
  })
      .then(response => {
        console.log(response.data);
        alert('举报成功！')
        // 提交后隐藏弹框
        showReportDialog.value = false;
        // 重置选中的举报理由
        selectedReportReason.value = '';
      })
      .catch(error => {
        console.error('提交举报失败:', error);
        alert('举报失败！')
      });
};

const formatNumber = (num: number): string => {
  if (!num) return '0'
  if (num >= 10000) {
    return (num / 10000).toFixed(1) + '万'
  }
  return num.toString()
}

const goBack = () => {
  router.back()
}

const handleReport = () => {
  // 新增：显示举报理由弹框
  showReportDialog.value = true
}

const handleCancel = () => {
  showOptions.value = false
  // 新增：隐藏举报理由弹框
  showReportDialog.value = false
  // 重置选中的举报理由
  selectedReportReason.value = ''
}

const showMoreOptions = () => {
  showOptions.value = true
}

// const submitReport = () => {
//   // 这里可以添加提交举报的逻辑，例如发送axios请求到后端
//   console.log('提交举报，理由：', selectedReportReason.value)
//   // 提交后隐藏弹框
//   showReportDialog.value = false
//   // 重置选中的举报理由
//   selectedReportReason.value = ''
// }

// 用于跳转到作品详情页
const goToWorkDetail = (workId: number) => {
  router.push({ name: 'WorkDetail', params: { id: workId } })
}

onMounted(() => {
  fetchUserContent()
})
</script>

<template>
  <div class="author-page">
    <!-- 顶部导航栏 -->
    <div class="nav-bar">
      <div class="back-btn" @click="goBack">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
          <path fill="currentColor" d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z" />
        </svg>
      </div>
      <div class="more-btn" @click="showMoreOptions">
        <!-- 替换为三个点的SVG图标并设置为白色 -->
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
          <circle cx="6" cy="12" r="2" fill="white" />
          <circle cx="12" cy="12" r="2" fill="white" />
          <circle cx="18" cy="12" r="2" fill="white" />
        </svg>
      </div>
    </div>

    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">加载失败</div>
    <template v-else>
      <!-- 用户信息区域 -->
      <div class="user-info">
        <div class="avatar">
          <img :src="userInfo.avatar" :alt="userInfo.name">
        </div>
        <div class="name">{{ userInfo.name }}</div>
        <div class="description">{{ userInfo.description }}</div>

        <!-- 统计信息 -->
        <div class="stats">
          <div class="stat-item">
            <div class="number">{{ userInfo.stats.follows }}</div>
            <div class="label">关注</div>
          </div>
          <div class="stat-item">
            <div class="number">{{ userInfo.stats.fans }}</div>
            <div class="label">粉丝</div>
          </div>
          <div class="stat-item">
            <div class="number">{{ userInfo.stats.likes }}</div>
            <div class="label">获赞</div>
          </div>
        </div>

<!--        <button class="follow-btn">回关</button>-->
      </div>

      <!-- 作品列表 -->
      <div class="works-grid">
        <div v-for="work in works" :key="work.id" class="work-card" @click="goToWorkDetail(work.id)">
          <div class="work-image">
            <img :src="work.image" :alt="work.title">
          </div>
          <div class="work-info">
            <div class="work-title">{{ work.title }}</div>
            <div class="work-stats">
              <div class="author">
                <img :src="userInfo.avatar" :alt="userInfo.name">
                <span>{{ userInfo.name }}</span>
              </div>
              <div class="actions">
                <span class="like">❤️ {{ formatNumber(work.likes) }}</span>
                <span class="share">↗️ {{ formatNumber(work.shares) }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </template>

    <!-- 弹出菜单 -->
    <div v-if="showOptions" class="options-menu">
      <div class="menu-overlay" @click="handleCancel"></div>
      <div class="menu-content">
        <div class="menu-item" @click="handleReport">我要举报</div>
        <div class="menu-item" @click="handleCancel">取消</div>
      </div>
    </div>

    <!-- 新增：举报理由弹框 -->
    <div v-if="showReportDialog" class="report-dialog">
      <div class="dialog-overlay" @click="handleCancel"></div>
      <div class="dialog-content">
        <h3>举报理由</h3>
        <select v-model="selectedReportReason">
          <option v-for="(reason, index) in reportReasons" :key="index">{{ reason }}</option>
        </select>
        <div class="button-group">
          <button @click="handleCancel">取消</button>
          <button @click="submitReport">提交</button>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.author-page {
  min-height: 100vh;
  background: linear-gradient(180deg, rgba(0, 0, 0, 0.3) 0%, #322020 300px),
  linear-gradient(90deg, #f5a8f0 0%, #7ecfff 100%);
  padding-bottom: 20px;
}

.nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.back-btn,
.more-btn {
  width: 24px;
  height: 24px;
  cursor: pointer;
  color: white;
}

.user-info {
  padding: 60px 20px 20px;
  text-align: center;
  color: white;
}

.avatar {
  width: 80px;
  height: 80px;
  margin: 0 auto 12px;
}

.avatar img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
}

.name {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 8px;
}

.description {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  margin-bottom: 16px;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-bottom: 20px;
}

.stat-item {
  text-align: center;
}

.number {
  font-size: 18px;
  font-weight: 600;
}

.label {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  margin-top: 4px;
}

.follow-btn {
  background: #007AFF;
  color: white;
  border: none;
  padding: 8px 32px;
  border-radius: 20px;
  font-size: 14px;
  cursor: pointer;
}

.works-grid {
  padding: 12px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.work-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
}

.work-image {
  width: 100%;
  padding-top: 100%;
  position: relative;
}

.work-image img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.work-info {
  padding: 8px;
}

.work-title {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.work-stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.author {
  display: flex;
  align-items: center;
  gap: 4px;
}

.author img {
  width: 20px;
  height: 20px;
  border-radius: 50%;
}

.actions {
  display: flex;
  gap: 12px;
  color: #666;
}

.options-menu {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.menu-content {
  position: relative;
  background: white;
  border-radius: 12px 12px 0 0;
  overflow: hidden;
}

.menu-item {
  padding: 16px;
  text-align: center;
  border-bottom: 1px solid #eee;
  cursor: pointer;
}

.menu-item:last-child {
  border-bottom: none;
}

.loading,
.error {
  text-align: center;
  padding: 20px;
  color: white;
}

/* 新增：举报理由弹框样式 */
.report-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1001;
}

.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
}

.dialog-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: white;
  border-radius: 12px;
  padding: 20px;
  width: 300px;
}

h3 {
  text-align: center;
  margin-bottom: 15px;
}

select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin-bottom: 15px;
}

.button-group {
  display: flex;
  justify-content: space-between;
}

button {
  padding: 8px 16px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}

button:nth-child(1) {
  background: #ccc;
  color: white;
}

button:nth-child(2) {
  background: #007AFF;
  color: white;
}
</style>
